//
//  ITTabBarController.m
//  InTubePro
//
//  Created by seven on 2019/12/19.
//  Copyright © 2019 seven. All rights reserved.
//

#import "ITTabBarController.h"


@interface ITTabBarController ()<UITabBarControllerDelegate>{
    BaseView *_selBg;
    UIButton *_selItem;
    BaseLabel *_selLbl;
    
    BaseLabel *_point;
}
IT_PROPERTY_STRONG NSMutableArray *items;
IT_PROPERTY_STRONG NSMutableArray *tabbarFrames;
@end

@implementation ITTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[NSNotificationCenter addObserver:self selector:@selector(updateUnreadMessage) name:UIApplicationWillEnterForegroundNotification];
    
    [NSNotificationCenter addObserver:self selector:@selector(didLogin) name:NotificationDidLogin];
    [NSNotificationCenter addObserver:self selector:@selector(didBecomeActive) name:UIApplicationWillEnterForegroundNotification];
    [NSNotificationCenter addObserver:self selector:@selector(willResignActive) name:UIApplicationDidEnterBackgroundNotification];
    
    self.delegate = self;
    self.fd_prefersNavigationBarHidden = YES;
    
    //*
    UIColor *color = ColorWithHex(0x000000);
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage new];
    self.tabBar.barTintColor = color;
    //[self.tabBar setBorderColor:ColorWithHex(0xBBBBBB) borderWidth:0.5];
    [self.tabBar addLineWithFrame:CGRectMake(0, 0, kScreenWidth, .5) color:ColorWithHexAplha(0xF4F4F4, 0.21)];
    self.tabBar.translucent = NO;
    //*/
    [self initlizedViewControllers];
    
    _selBg = [BaseView initWithFrame:CGRectMake(0, 12, 46, 46)];
    _selBg.backgroundColor = color;
    [_selBg setCornerRadius:_selBg.height/2];
    [_selBg setBorderColor:ColorWithHexAplha(0xF4F4F4, 0.21) borderWidth:1];
    [self.view insertSubview:_selBg belowSubview:self.tabBar];
    
    _selItem = [UIButton initWithFrame:CGRectInset(_selBg.frame, 1, 1)];
    _selItem.contentEdgeInsets = UIEdgeInsetsMake(4, 4, 4, 4);
    [_selItem addTarget:self action:@selector(onDoubleClick)];
    [self.view addSubview:_selItem];
    _selItem.backgroundColor = color;
    _selItem.layer.cornerRadius = _selItem.height/2;
    _selItem.layer.masksToBounds = NO;
    
    CGFloat left = -30;
    _selLbl = [BaseLabel initWithFrame:CGRectMake(left, 40, _selItem.width-left*2, 17)];
    _selLbl.font = SemiboldFontWithSize(12);
    _selLbl.backgroundColor = _selItem.backgroundColor;
    //_selLbl.textColor = ColorWithHex(0x4DA5F7);
    _selLbl.textColor = ColorWithHex(0xFFD000);
    _selLbl.textAlignment = NSTextAlignmentCenter;
    [_selItem addSubview:_selLbl];
    
    [self updateSelItem];
}


-(void)initlizedViewControllers{
    self.selectedIndex = 0;
    //return;
    if ([AppUtils.findRootController isKindOfClass:UINavigationController.class]) {
        [AppUtils.findRootController popToRootViewControllerAnimated:NO];
    }
    NSMutableArray *views = NSMutableArray.array;
    self.items = NSMutableArray.array;
    
    BaseViewController *_home = BaseViewController.new;
    //_home.viewShowed = YES;
    [_items addObject:[ITTabarModel modelWithTitle:LocalTitle(@"Home") img:@"tabbar_home" img2:@"tabbar_home2" index:WTTabBarIndexHome]];
    [views addObject:_home];
    
    BaseViewController *_user = [BaseViewController new];
    [_items addObject:[ITTabarModel modelWithTitle:LocalTitle(@"Account") img:@"tabbar_user" img2:@"tabbar_user2" index:WTTabBarIndexAccount]];
    [views addObject:_user];
    
    self.viewControllers = views;
    
    for (int i=0; i<views.count; i++) {
        UIViewController *v = views[i];
        ITTabarModel *m = _items[i];
        [self updateItem:v.tabBarItem title:m.title image:m.image image2:nil];
    }
    
    NSMutableArray *tmp = NSMutableArray.array;
    for (UIView *view in self.tabBar.subviews) {
        if ([view.className isEqualToString:@"UITabBarButton"]) {
            [tmp addObject:@(view.centerX)];
        }
    }
    _tabbarFrames = tmp;
    
    //self.selectedIndex = 1;
     //*/
}

- (void)updateItem:(UITabBarItem *)item title:(NSString *)title image:(UIImage *)image image2:(UIImage *)image2{
    item.title = title;
    [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
    [item setImage:image];
    [item setSelectedImage:nil];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: COLOR_CLEAR} forState:(UIControlStateNormal)];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: COLOR_CLEAR} forState:(UIControlStateSelected)];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: COLOR_CLEAR} forState:(UIControlStateHighlighted)];
}


- (void)onDoubleClick{
    [self.selectedViewController onDoubleClick];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[ITOrientationManager.sharedInsatnce changeLanscape:YES];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    _selBg.top = self.tabBar.top-12;
    _selItem.centerY = _selBg.centerY;
    [self.view insertSubview:_selBg belowSubview:self.tabBar];
    [self.view insertSubview:_selItem aboveSubview:self.tabBar];
    
    [self updateSelItem];
}


- (void)updateSelItem{
    CGFloat centerX = [_tabbarFrames[self.selectedIndex] floatValue];
    _selBg.centerX = _selItem.centerX = centerX;
    ITTabarModel *m = _items[self.selectedIndex];
    [_selItem setImage:m.selImage];
    _selLbl.text = m.title;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    
    [self updateSelItem];
}

-(void)showBannerView{
    //[_home showBannerView];
}



#pragma mark - Active
- (void)didBecomeActive{
    UIViewController *top = [UIViewController currentTopViewController];
    if (top == (id)self) {
        top = (id)self.selectedViewController;
    }
    [top didBecomeActive];
}

- (void)willResignActive{
    UIViewController *top = [UIViewController currentTopViewController];
    if (top == (id)self) {
        top = (id)self.selectedViewController;
    }
    [top willResignActive];
}

- (void)didLogin{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSUInteger selIdx = [self.viewControllers indexOfObject:viewController];
    ITTabarModel *m = _items[selIdx];
    [self updateSelItem];
    [AppUtils trackMTAEventNo:@"vq_00023" parameters:@{@"sequence": @(selIdx).stringValue, @"name": m.title}];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger idx = [self.viewControllers indexOfObject:viewController];
    if (self.selectedIndex == idx) {
        [viewController onDoubleClick];
        return YES;
    }
    return YES;
}

@end
